﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;
using System.Data.SQLite;
namespace libry
{
    class BookBorrowServer
    {
        //借书
        public static void borrowBook(int userId, String bookLocatedId)
        {
            //检查book_located中borrowed=0
            try
            {
                DataTable dt = sqlUser.getBookLocatedById(bookLocatedId);
                int bookid = Convert.ToInt32(dt.Rows[0]["bookid"]);
                bool borrrowed = (bool)dt.Rows[0]["borrowed"];
                if (!borrrowed)
                {
                    //检查借阅表中这位用户所借阅书的数量是否小于limitNum
                    String table = "bookBorrowWithName";
                    DataTable userDt = sqlUser.getUserById(userId);
                    String userEmail = (String)userDt.Rows[0]["userEmail"];
                    int limitNum = Convert.ToInt32(userDt.Rows[0]["limitNum"]);
                    int limitDays = Convert.ToInt32(userDt.Rows[0]["limitDays"]);
                    DataRow[] rows = sqlUser.queryBookBorrow(table, "", userEmail, "借阅中");
                    int count = 0;
                    if (rows != null)
                    {
                        count = rows.Length;
                    }
                    //增加借阅表
                    if (count < limitNum)
                    {
                        int id = 1;
                        //设置借阅表id
                        DataTable dt1 = new DataTable();
                        dt1 = sqlUser.getBookBorrowAll();
                        if (dt1.Rows.Count != 0)
                        {
                            int maxId = Convert.ToInt32(dt1.Compute("Max(id)", "true")) + 1;
                            id = maxId;
                        }
                        DateTime beginT = DateTime.Now;
                        String beginTime = beginT.ToString();
                        DateTime endT = beginT.AddDays(limitDays);
                        String endTime = endT.ToString();
                        String returned = "借阅中";
                        SQLiteConnection con = sqlUser.Con;
                        con.Open();
                        SQLiteTransaction tr = con.BeginTransaction();
                        try
                        {
                            sqlUser.addBookBorrow(id, userId, bookid, bookLocatedId, beginTime, endTime, returned);
                            //更改book_located
                            String changeText = "borrowed = 1";
                            sqlUser.ChangeBookLocated(bookLocatedId, changeText);
                            tr.Commit();
                            con.Close();
                            MessageBox.Show("借书成功");
                        }
                        catch (Exception ex)
                        {
                            tr.Rollback();
                            con.Close();
                            MessageBox.Show("事务回滚成功");

                        }
                    }
                    else
                    {
                        MessageBox.Show("该用户借书已达上限");
                    }
                }
                else
                {
                    MessageBox.Show("该书已被借阅，可能是数据库出错");

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("借书出错，可能是没有这本书" + ex);
            }
        }
        public static void returnedBook(int userId, String bookLocatedId)
        {
            try
            {
                //是否借阅
                DataTable dt = sqlUser.getBookBorrowAll();
                DataRow[] dr = dt.Select("bookLocatedId = '" + bookLocatedId + "' and userId = '" + userId + "' and returned = '借阅中'");
                if (dr.Length > 0)
                {
                    SQLiteConnection con = sqlUser.Con;
                    con.Open();
                    SQLiteTransaction tr = con.BeginTransaction();
                    try
                    {
                        int id = Convert.ToInt32(dr[0]["id"]);
                        String change = "borrowed =0";
                        sqlUser.ChangeBookLocated(bookLocatedId, change);
                        String returnedTime = DateTime.Now.ToString();
                        sqlUser.ChangeBookBorrow(id, "returned = '已归还',endTime = '" + returnedTime + "'");
                        tr.Commit();
                        con.Close();
                        MessageBox.Show("还书成功！");
                    }
                    catch (Exception ex)
                    {
                        tr.Rollback();
                        con.Close();
                        MessageBox.Show("事务回滚成功");
                    }
                }
                else
                {
                    MessageBox.Show("书和用户匹配不上或者该书已经归还");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("还书出错，可能是没有这本书" + ex);
            }

        }
    }
}
